package com.book.crud;
import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.sample.portlet.library.model.Book;
import com.sample.portlet.library.service.BookLocalServiceUtil;

public class CRUDUtil extends MVCPortlet {

	@Override
	public void processAction(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {
		Long bookId = ParamUtil.getLong(actionRequest,"bookId");
		String bookName = ParamUtil.getString(actionRequest, "bookName");
		Double version = ParamUtil.getDouble(actionRequest, "version");
		Double price = ParamUtil.getDouble(actionRequest, "price");
		String author = ParamUtil.getString(actionRequest, "author");
		
		if(bookId <= 0){
			try {
				bookId = CounterLocalServiceUtil.increment();
			} catch (SystemException e) {
				e.printStackTrace();
			}
		}
			Book book = BookLocalServiceUtil.createBook(bookId); 
			book.setBookname(bookName);
			book.setVersion(version);
			book.setAuthor(author);
			book.setPrice(price);
			try {
				BookLocalServiceUtil.addBook(book);
				SessionMessages.add(actionRequest, "bookAdded");
			} catch (SystemException e) {
				e.printStackTrace();
			}
	}
	@Override
	public void render(RenderRequest request, RenderResponse response)
			throws PortletException, IOException {
		
		super.render(request, response);
	}
}
